package com.hnevc.netdemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

public class ClientSocketDemo {

	public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
		Socket socket = new Socket("127.0.0.1",20000);
		//发送
		OutputStream out = socket.getOutputStream();
		String data ="来自客户端的数据。你好，我的朋友！";
		out.write(data.getBytes());
		socket.shutdownOutput();
		//接收
		InputStream in = socket.getInputStream();
		byte[] buffer = new byte[1024];
		int len = in.read(buffer);
		data = new String(buffer,0,len);
		System.out.println(data);
		
		socket.close();
	}
}
